package ru.teleology.cubefs.api.reflection.value;

public final class Version {
	private int magor, minor, patchset, timestamp;
	
	public Version() {}
	
	public Version(String value) {
		if (value != null) 
		{
			String[] tokens = value.trim().split(".");
			
			for (int c = 0; c < tokens.length; c++) 
			{
				switch (c) {
				case 0: magor = Integer.parseInt(tokens[c].trim()); 		break;
				case 1: minor = Integer.parseInt(tokens[c].trim()); 		break;
				case 2: patchset = Integer.parseInt(tokens[c].trim());		break;
				case 3: timestamp = Integer.parseInt(tokens[c].trim());		break;
				default: throw new RuntimeException("Invalud version format: " + value);
				}
			}
		}
		else {
			throw new NullPointerException("Argument must not be null");
		}
	}
	
	public int GetMagor() {
		return magor;
	}

	public int GetMinor() {
		return minor;
	}

	public int GetPatchset() {
		return patchset;
	}

	public int GetTimestamp() {
		return timestamp;
	}
	
	public boolean IsCompatible(Version other) {
		return GetMagor() == other.GetMagor() && GetMinor() <= other.GetMinor();
	}
	
	public boolean equals (Object o) {
		if (o instanceof Version) 
		{
			Version other = (Version) o; 
			return magor == other.magor && minor == other.minor && patchset == other.patchset && timestamp == other.timestamp;
		}
		else {
			return false;
		}
	}
	

	public boolean IsMatches(Version other) 
	{
		return (magor 		!= 0 ? magor 		== other.magor : true) &&
				(minor  	!= 0 ? minor 		<= other.minor : true)  &&
				(patchset 	!= 0 ? patchset		<= other.patchset : true) &&
				(timestamp	!= 0 ? timestamp	<= other.timestamp : true);
	}
	
	public String toString() {
		return magor+"."+minor+"."+patchset+"."+timestamp;
	}	
}
